﻿using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    public  class SheepMarkService : ISheepMarkService 
    {
        private readonly IBaseRepository<SheepMark> _sheepMarkRepository;

        public SheepMarkService(IBaseRepository<SheepMark> sheepMarkRepository)
        {
            _sheepMarkRepository = sheepMarkRepository;
        }
        /// <summary>
        /// 戴耳列表
        /// </summary>
        /// <param name="overBit"></param>
        /// <param name="elEarTag"></param>
        /// <param name="newElEarTag"></param>
        /// <param name="taggingReason"></param>
        /// <param name="labeledState"></param>
        /// <returns></returns>
        public  async Task<PageOutput<SheepMark>> GetSheepMarks(int pageIndex, int pageSize, string? overBit, string? elEarTag, string? newElEarTag, int taggingReason, int labeledState)
        {
            var list = _sheepMarkRepository.GetAll().Where(c => c.IsDel == 0);
            if (!string.IsNullOrEmpty(overBit))
            {
                list = list.Where(c => c.OverBit.Contains(overBit));
            }
            if(!string.IsNullOrEmpty(elEarTag))
            {
                list=list.Where(c => c.ElEarTag.Contains(elEarTag));
            }
            if (!string.IsNullOrEmpty(newElEarTag))
            {
                list = list.Where(c => c.NewElEarTag.Contains(newElEarTag));
            }
            if (taggingReason != -1)
            {
                list = list.Where(c => c.TaggingReason == taggingReason);
            }
            if (labeledState!= -1)
            {
                list = list.Where(c => c.LabeledState == labeledState);
            }
            //创建分页对象
            var result = new PageOutput<SheepMark>()
            {
                Total = list.Count(),
                PageSize = pageSize,
            };
            //分页
            var slist=list.OrderByDescending(c=>c.SheepMarkId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            result.Data = slist;
            return result;
        }
        /// <summary>
        /// 戴耳添加
        /// </summary>
        /// <param name="sheepMark"></param>
        /// <returns></returns>
        public async Task<int> AddSheepMarks(SheepMark sheepMark)
        {
            var result = new SheepMark()
            {
                SheepMarkId = sheepMark.SheepMarkId,
                OverBit=sheepMark.OverBit,
                ElEarTag=sheepMark.OverBit,
                NewElEarTag=sheepMark.NewElEarTag,
                OldElEarTag=sheepMark.OldElEarTag,
                TaggingReason=sheepMark.TaggingReason,
                LabeledState=sheepMark.LabeledState,
            };
            return _sheepMarkRepository.Add(result);

        }
        /// <summary>
        /// 戴标删除
        /// </summary>
        /// <param name="sheepMarkId"></param>
        /// <returns></returns>
        public async Task<int> DelSheepMarks(int sheepMarkId)
        {
            var list = _sheepMarkRepository.GetInfoBy(sheepMarkId);
            list.IsDel = 1;
            return _sheepMarkRepository.Update(list);
        }
        /// <summary>
        /// 戴标批删
        /// </summary>
        /// <param name="SheepMarkIds"></param>
        /// <returns></returns>
        public async Task<int> BatchDelSheepMarks(List<int> sheepMarkIds)
        {
            foreach(var id in sheepMarkIds)
            {
                var list = _sheepMarkRepository.GetInfoBy(id);
                list.IsDel = 1;
                _sheepMarkRepository.Update(list);
            }
            return 1;
        }
        /// <summary>
        /// 戴标反填
        /// </summary>
        /// <param name="SheepMarkId"></param>
        /// <returns></returns>
        public async Task<SheepMark> GetSheepMark(int sheepMarkId)
        {
            var list = _sheepMarkRepository.GetInfoBy(sheepMarkId);
            return list;
        }
        /// <summary>
        /// 戴标修改
        /// </summary>
        /// <param name="sheepMark"></param>
        /// <returns></returns>
        public async Task<int> UpdSheepMarks(SheepMark sheepMark)
        {
            var list = _sheepMarkRepository.GetInfoBy(sheepMark.SheepMarkId);
            //list.OverBit = sheepMark.OverBit;
            list.SheepMarkId = sheepMark.SheepMarkId;
            list.ElEarTag = sheepMark.ElEarTag;
            list.TaggingReason = sheepMark.TaggingReason;
            list.LabeledState = sheepMark.LabeledState;
            return _sheepMarkRepository.Update(list);
        }
    }
}
